package com.killer.edu.order.remote;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.killer.edu.common.constant.CacheDefine;
import com.killer.edu.common.constant.MQConstant;
import com.killer.edu.common.util.ConverUtil;
import com.killer.edu.common.util.ValidateUtils;
import com.killer.edu.course.api.CourseRemoteService;
import com.killer.edu.course.api.dto.ActivityCourseDTO;
import com.killer.edu.course.api.dto.ActivityCourseUpdateStockDTO;
import com.killer.edu.course.api.dto.CourseDTO;
import com.killer.edu.course.api.enums.CourseStatus;
import com.killer.edu.order.api.UserCourseOrderRemoteService;
import com.killer.edu.order.api.dto.CreateShopGoodsOrderReqDTO;
import com.killer.edu.order.api.dto.UserCourseOrderDTO;
import com.killer.edu.order.api.dto.UserCourseOrderResDTO;
import com.killer.edu.order.api.enums.UserCourseOrderStatus;
import com.killer.edu.order.entity.UserCourseOrder;
import com.killer.edu.order.service.IUserCourseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/userCourseOrder")
@Slf4j
public class UserCourseOrderRemoteServiceImpl implements UserCourseOrderRemoteService {

    @Autowired
    private IUserCourseOrderService userCourseOrderService;

    @Override
    @GetMapping("/getUserCourseOrderByUserId")
    public List<UserCourseOrderDTO> getUserCourseOrderByUserId(Integer userId) {
        ValidateUtils.notNullParam(userId);
        ValidateUtils.isTrue(userId > 0, "用户id错误");
        List<UserCourseOrder> userCourseOrderList = userCourseOrderService.list(new QueryWrapper<UserCourseOrder>().eq("user_id", userId).eq("status", UserCourseOrderStatus.SUCCESS.getCode()).orderByDesc("id"));
        if(CollectionUtils.isEmpty(userCourseOrderList)) {
            return Lists.newArrayList();
        }
        return ConverUtil.convertList(userCourseOrderList, UserCourseOrderDTO.class);
    }

    @Override
    @GetMapping("/getOrderListByCourseId")
    public List<UserCourseOrderDTO> getOrderListByCourseId(Integer coursId) {
        ValidateUtils.notNullParam(coursId);
        ValidateUtils.isTrue(coursId > 0, "课程id错误");
        List<UserCourseOrder> userCourseOrderList = userCourseOrderService.list(new QueryWrapper<UserCourseOrder>().eq("course_id", coursId).eq("status", UserCourseOrderStatus.SUCCESS.getCode()));
        if(CollectionUtils.isEmpty(userCourseOrderList)) {
            return Lists.newArrayList();
        }
        return ConverUtil.convertList(userCourseOrderList, UserCourseOrderDTO.class);
    }
}
